package com.tyk.bookstore.front.product.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.common.core.model.vo.PageVo;
import com.tyk.bookstore.front.product.model.query.CommonSearchQuery;
import com.tyk.bookstore.front.product.model.vo.BookPageVo;
import com.tyk.bookstore.front.product.service.SearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author tyk
 * @description 商品搜素
 */
@RequestMapping({"api/product/search"})
@Tag(name = "商品管理/商品搜索", description = "商品搜索")
@RestController
@RequiredArgsConstructor
public class SearchController {
    private final SearchService searchService;

    @GetMapping({"common"})
    @Operation(summary = "通用搜索", description = "通用搜索")
    public R<BookPageVo> commonSearch(
            @Validated @ParameterObject CommonSearchQuery query,
            BindingResult br
    ) {
        if (br.hasErrors()) return R.ok(PageVo.instance(BookPageVo.class));
        return R.ok(searchService.commonSearch(query));
    }
}
